/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.airlogger.proxy.view
{
	import com.bourre.log.PixlibDebug;	
	
	import flash.xml.XMLDocument;	
	
	import com.airlogger.event.AirLoggerLogEvent;
	import com.airlogger.proxy.AirLoggerProxyEventList;
	import com.airlogger.utils.ConnectionUtils;
	import com.bourre.plugin.Plugin;
	import com.bourre.view.AbstractView;
	
	import flash.events.AsyncErrorEvent;
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;
	
	import com.airlogger.event.SWFEvent;

import flash.utils.getQualifiedClassName;	public class AirLoggerProxyFacade extends AbstractView
	{
		protected var _lcIn : LocalConnection;
		protected var _lcOut : LocalConnection;
				
		public function AirLoggerProxyFacade( owner : Plugin = null, id : String = null )
		{
			super( owner, id );
			
			//getLogger().debug( "("+getName()+") facade instanciated ");
			
			_lcIn = new LocalConnection();
			_lcIn.client = this;
			_lcIn.allowDomain( "*" );
			_lcIn.connect( ConnectionUtils.getInConnectionName( getName() ) );
			_lcIn.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onASyncError, false, 0, true);
			
			_lcOut = new LocalConnection();
			_lcOut.addEventListener( StatusEvent.STATUS, onStatus, false, 0, true );
		}
		
		public function log ( o : * ) : void
		{
			//getLogger().debug( "("+getName()+") log called " + o.message + ", type : " + o.messageType + ", " + getQualifiedClassName( o.message ) );
			
			if ( getQualifiedClassName( o.message) == "flash.xml::XMLDocument" )
			{
				o.message = new XML( (o.message as XMLDocument ).toString() );
				o.messageType = "XML";
			}
			if( o.message == null )
			{
				o.message = "null";
				o.messageType = "String";
			}
			
			var evt : AirLoggerLogEvent = new AirLoggerLogEvent ( 
					  AirLoggerProxyEventList.onAddLogEVENT, this, getName(), o );
			getOwner().firePublicEvent( evt );
		}
		
		public function focus ( o : * ) : void
		{
			//getLogger().debug( "("+getName()+") focus called " + o );
			
			var evt : AirLoggerLogEvent = new AirLoggerLogEvent ( 
					  AirLoggerProxyEventList.onFocusEVENT, this, getName() );
			getOwner().firePublicEvent( evt );
		}
		
		public function clear ( o : * ) : void
		{
			//getLogger().debug( "("+getName()+") clear called " + o );
			
			var evt : AirLoggerLogEvent = new AirLoggerLogEvent ( 
					  AirLoggerProxyEventList.onClearLogsEVENT, this, getName() );
			getOwner().firePublicEvent( evt );
		}
		
		public function setTabName ( name : String ) : void
		{
			getOwner().firePublicEvent( new SWFEvent( AirLoggerProxyEventList.onTabNameChangedEVENT, this, getName(), name ) );
		}
		
		public function close():void
		{
			_lcIn.close( );
			try
			{
				_lcOut.send( ConnectionUtils.getOutConnectionName( getName() ), "close" );
			}
			catch( e : Error ){};
			
		}

		public function onASyncError ( e : AsyncErrorEvent ) : void
		{
			//getLogger().error( e.target + " have emit " + e.error ); 
		}
		
		public function onStatus ( e : StatusEvent ) : void
		{
		}
	}
}